# task_master.py
import datetime
import queue
import time
from multiprocessing import Queue
from multiprocessing.managers import BaseManager

# 从BaseManager继承的QueueManager:
import facade
from xjlibrary.our_file_dir import BaseDir
from xjlibrary.tools.BaseIp import GetLocalIPByPrefix

curPath = BaseDir.get_file_dir_absolute(__file__)
curPath = BaseDir.get_upper_dir(curPath, -1)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class QueueManager(BaseManager):
    pass


class SendTask(object):
    def __init__(self):
        # 发送任务的队列:
        self.task_queue = Queue(1000)
        # 接收结果的队列:
        self.result_queue = Queue()
        self.register()
        self.ip = GetLocalIPByPrefix("192.168.")
        # 绑定端口5000, 设置验证码'abc':
        self.manager = QueueManager(address=(self.ip, 5001), authkey=b'abc')
        self.task = None
        self.result = None

    def register(self):
        # 把两个Queue都注册到网络上, callable参数关联了Queue对象:
        QueueManager.register('get_task_queue', callable=self.return_task_queue)
        QueueManager.register('get_result_queue', callable=self.return_result_queue)

    def return_task_queue(self):
        return self.task_queue

    def return_result_queue(self):
        return self.result_queue

    def start(self):
        # 启动Queue:
        self.manager.start()

    def set_task_result_obj(self):
        # 获得通过网络访问的Queue对象:
        self.task = self.manager.get_task_queue()
        self.result = self.manager.get_result_queue()

    def set_task(self, n):
        print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
        print(n)
        self.task.put(n)

    def get_result(self):
        # 从result队列读取结果:
        print('Try get results...')
        try:
            for i in range(10):
                r = self.result.get(timeout=10)
                print('Result: %s' % r)
        except queue.Empty:
            print('task queue is empty.')

    def shutdown(self):
        # 关闭:
        self.manager.shutdown()
        print('master exit.')


class SelectTask(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)

    def selectDB(self):
        """
        获取page表状态为0的数据
        :return:
        """
        sql = "select pub_date,`page`,parameter from `page` where stat<=0 ORDER BY pub_date ASC limit 500"
        rows = self.mysqlutils.SelectFromDB(sql)
        return rows

    def select_date(self):
        """
        当需要下一个日期的数据时使用该sql
        :return:
        """
        sql = "select pub_date from `page` GROUP BY pub_date ORDER BY pub_date ASC limit 1"
        rows = self.mysqlutils.SelectFromDBFetchOne_noyield(sql)
        return rows

    def select_date2(self):
        sql = "select `startdate`, `enddate` from `needdownpage` where `stat`=0"
        rows = self.mysqlutils.SelectFromDB(sql)
        return rows

    def selectDB3(self):
        """
        获取page表状态为0的数据
        :return:
        """
        sql = "select para,`page`,parameter from `pagepara` where stat=0 ORDER BY `para` ASC limit 2000"
        rows = self.mysqlutils.SelectFromDB(sql)
        return rows


if __name__ == "__main__":
    sendtask = SendTask()
    select = SelectTask()
    sendtask.start()
    sendtask.set_task_result_obj()
    while True:
        if sendtask.task.qsize() != 0:
            print(sendtask.task.qsize())
            time.sleep(20)
            continue
        # rows = select.selectDB()
        # if rows:
        #     for row in rows:
        #         # 下载某一页
        #         sendtask.set_task(("down_page", row[0], row[1], row[2]))
        # else:
        #     row = select.select_date()
        #     if row:
        #         # 下载某一天
        #         print("row[0] is:" + row[0])
        #         sendtask.set_task(("down_search_page", beforday(row[0])))
        # sendtask.set_task(("down_search_page", "20180501"))
        # else:
        #     #
        #     sendtask.set_task(("down_search_page", "20181201"))
        #     del row
        # del rows
        ###########
        """
        rows = select.select_date2()
        if rows:
            for row in rows:
                sendtask.set_task(("down_search_page", row[0], row[1]))
        else:
            rows = select.selectDB()
            if rows:
                for row in rows:
                    # 下载某一页
                    sendtask.set_task(("down_page", row[0], row[1], row[2]))
        """

        #sendtask.set_task(("down_search_page", "", "", "申请号=(CN2009303+)"))
        #continue
        time.sleep(30)
        rows = select.selectDB3()
        if rows:
            for row in rows:
                # 下载某一页
                sendtask.set_task(("down_page", row[0], row[1], row[2]))
        else:
            pass


        # else:
        #     num = 93
        #     while True:
        #         sql = "select para from pagepara where para='申请号=(CN{}+)'".format(str(num))
        #         rows = select.mysqlutils.SelectFromDB(sql)
        #         if len(rows) == 0:
        #             break
        #         else:
        #             num += 1
        #
        #     sendtask.set_task(("down_search_page", "", "","申请号=(CN{}+)".format(str(num))))

